package com.example.business.controller;

import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.example.base.utils.IPUtils;
import com.example.business.entity.SysVideoPlayRecord;
import com.example.business.service.SysVideoPlayRecordService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import com.example.base.limit.IPRateLimit;

import java.util.Date;
import java.util.List;
import com.example.base.utils.Result;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import com.example.business.service.SysVideoService;
import com.example.business.entity.SysVideo;
import java.io.Serializable;
import java.util.Map;

import org.springframework.stereotype.Controller;

import javax.servlet.http.HttpServletRequest;

/**
 * <p>
 * 视频表 前端控制器
 * </p>
 *
 * @author xiong
 * @since 2024-01-27
 */

@RestController
@Api(tags = {"视频表的服务类"})
@RequestMapping("/sys-video")
public class SysVideoController {

        @Autowired
        private SysVideoService sysVideoService;

        @Autowired
        private SysVideoPlayRecordService sysVideoPlayRecordService;

        @GetMapping("scan")
        public JSONObject scan() throws Exception {
                sysVideoService.scanVideo();
                return Result.successInfo("扫描成功");
        }

        @PostMapping("/saveorupdate")
        @IPRateLimit(name = "SysVideo-saveorupdate",msg = "系统检测到ip异常访问行为，已暂停访问")
        @ApiOperation(value = "视频表的更新和新增接口",notes = "视频表的更新和新增接口")
        public JSONObject save(@RequestBody SysVideo sysVideo) {
                boolean flag= sysVideoService.saveOrUpdate(sysVideo);
                if(flag){
                        return Result.successInfo("保存成功");
                }else {
                        return Result.errorInfo("保存失败");
                }
        }

        @DeleteMapping("/{id}")
        @IPRateLimit(name = "SysVideo-del",msg = "系统检测到ip异常访问行为，已暂停访问")
        @ApiOperation(value = "视频表的删除接口",notes = "视频表的删除接口")
        public JSONObject delete(@PathVariable("id") Serializable id) {
                boolean flag = sysVideoService.removeById(id);
                if(flag){
                        return Result.successInfo("删除成功");
                }else {
                        return Result.errorInfo("删除失败");
                }
        }

        @PostMapping("/del/batch")
        @IPRateLimit(name = "SysVideo-delbatch",msg = "系统检测到ip异常访问行为，已暂停访问")
        @ApiOperation(value = "视频表的批量删除接口",notes = "视频表的批量删除接口")
        public JSONObject deleteBatch(@RequestBody List<Integer> ids){
                boolean flag =  sysVideoService.removeByIds(ids);
                if(flag){
                        return Result.successInfo("删除成功");
                }else {
                        return Result.errorInfo("删除失败");
                }
        }

        @GetMapping("/getall")
        @IPRateLimit(name = "SysVideo-getall",msg = "系统检测到ip异常访问行为，已暂停访问")
        @ApiOperation(value = "视频表的获取全部接口",notes = "视频表的获取全部接口")
        public JSONObject findAll() {
                List<SysVideo> list= sysVideoService.list();
                return Result.successInfo(list);
        }

        @GetMapping("/{id}")
        @IPRateLimit(name = "SysVideo-getid",msg = "系统检测到ip异常访问行为，已暂停访问")
        @ApiOperation(value = "视频表的获取一个接口",notes = "视频表的获取一个接口")
        public JSONObject findOne(@PathVariable("id") Serializable id,
                                  HttpServletRequest request) {
                sysVideoService.updatePlayerNumById(id);
                SysVideo data= sysVideoService.getById(id);
                SysVideoPlayRecord sysVideoPlayRecord=new SysVideoPlayRecord();
                sysVideoPlayRecord.setFileid(data.getId().toString());
                sysVideoPlayRecord.setFilename(data.getFilename());
                sysVideoPlayRecord.setM3u8url(data.getFilem3u8());
                String ip=IPUtils.getIpAddr(request);
                sysVideoPlayRecord.setIp(ip);
                sysVideoPlayRecord.setCreatedate(new Date());
                sysVideoPlayRecordService.save(sysVideoPlayRecord);
                return Result.successInfo(data);
        }

        @ApiOperation("/获取视频列表")
        @GetMapping("getlist")
        public JSONObject getlist(@RequestParam("id")Serializable id,
                                  @RequestParam(value = "limit",defaultValue = "10")Integer limit){
           List<SysVideo> sysVideos=sysVideoService.getListOrderById(id, limit);
           return Result.successInfo(sysVideos);
        }

        @GetMapping("/page")
        @IPRateLimit(name = "SysVideo-page",msg = "系统检测到ip异常访问行为，已暂停访问")
        @ApiOperation(value = "视频表的分页接口",notes = "视频表的分页接口")
        public JSONObject findPage(@RequestParam(value = "pageNum",defaultValue = "1") Integer pageNum,
                                        @RequestParam(value = "pageSize",defaultValue = "10") Integer pageSize) {
                QueryWrapper<SysVideo> queryWrapper = new QueryWrapper<>();
                IPage<SysVideo> page=sysVideoService.page(new Page<>(pageNum, pageSize),queryWrapper);
                return Result.successInfo(page);
        }
}

